package com.qianfeng.qlm.handforireader.fragment;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qianfeng.qlm.handforireader.R;
import com.qianfeng.qlm.handforireader.activity.NewChapterActivity;
import com.qianfeng.qlm.handforireader.adapter.ComicRecycleAdapter;
import com.qianfeng.qlm.handforireader.model.Comic;
import com.qianfeng.qlm.handforireader.util.Constant;
import com.qianfeng.qlm.handforireader.util.UrlMaker;
import com.qianfeng.qlm.handforireader.view.MyScrollView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 * User:tadhg
 * Date:2015/3/23
 * Email:tadhg299@163.com
 */
public class RecommendFragment extends Fragment implements View.OnClickListener {

    public static final String TAG = RecommendFragment.class.getSimpleName();

    private RequestQueue mRequestQueue;
    private ComicRecycleAdapter recommendAdapter;
    private ComicRecycleAdapter newAdapter;
    private ComicRecycleAdapter hotAdapter;
    private ComicRecycleAdapter likeAdapter;
    private RecyclerView recommendRecycle;
    private RecyclerView newRecycle;
    private RecyclerView hotRecycle;
    private RecyclerView likeRecycle;

    public RecommendFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_racommend, container, false);

        final MyScrollView myScrollView = (MyScrollView) ret.findViewById(R.id.myScrollView);
        myScrollView.setCanMove(false);

        final RelativeLayout loadingLayout = (RelativeLayout) ret.findViewById(R.id.loading_layout);
        ImageView imageView = (ImageView) ret.findViewById(R.id.loading_animation);
        final AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable();
        animationDrawable.start();

        mRequestQueue = Volley.newRequestQueue(getActivity());
        recommendRecycle = (RecyclerView) ret.findViewById(R.id.recommend_comic);
        newRecycle = (RecyclerView) ret.findViewById(R.id.new_comic);
        hotRecycle = (RecyclerView) ret.findViewById(R.id.hot_comic);
        likeRecycle = (RecyclerView) ret.findViewById(R.id.like_comic);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getActivity());
        layoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);

        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getActivity());
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);

        LinearLayoutManager layoutManager3 = new LinearLayoutManager(getActivity());
        layoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL);

        recommendAdapter = new ComicRecycleAdapter(new ArrayList<Comic>(), getActivity(), this, "recommend");
        recommendRecycle.setLayoutManager(layoutManager);
        recommendRecycle.setAdapter(recommendAdapter);

        newAdapter = new ComicRecycleAdapter(new ArrayList<Comic>(), getActivity(), this, "new");
        newRecycle.setLayoutManager(layoutManager1);
        newRecycle.setAdapter(newAdapter);

        hotAdapter = new ComicRecycleAdapter(new ArrayList<Comic>(), getActivity(), this, "hot");
        hotRecycle.setLayoutManager(layoutManager2);
        hotRecycle.setAdapter(hotAdapter);

        likeAdapter = new ComicRecycleAdapter(new ArrayList<Comic>(), getActivity(), this, "like");
        likeRecycle.setLayoutManager(layoutManager3);
        likeRecycle.setAdapter(likeAdapter);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Constant.HOME_RECOMMEND_URL,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            Log.d(TAG,response.toString());
                            if (response != null) {

                                animationDrawable.stop();
                                loadingLayout.setVisibility(View.GONE);
                                myScrollView.setCanMove(true);

                                JSONArray recommendComicArr = response.getJSONArray("recommend_comic_arr");
                                JSONArray newComicArr = response.getJSONArray("new_comic_arr");
                                JSONArray hotComicArr = response.getJSONArray("hot_comic_arr");
                                JSONArray likeComicArr = response.getJSONArray("like_comic_arr");
                                Gson gson = new Gson();

                                List<Comic> recommendList = gson.fromJson(recommendComicArr.toString(), new TypeToken<List<Comic>>() {
                                }.getType());

                                recommendAdapter.addAll(recommendList);

                                List<Comic> newList = gson.fromJson(newComicArr.toString(),new TypeToken<List<Comic>>(){}.getType());

                                newAdapter.addAll(newList);

                                List<Comic> hotList = gson.fromJson(hotComicArr.toString(),new TypeToken<List<Comic>>(){}.getType());

                                hotAdapter.addAll(hotList);


                                List<Comic> likeList = gson.fromJson(likeComicArr.toString(),new TypeToken<List<Comic>>(){}.getType());

                                likeAdapter.addAll(likeList);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        mRequestQueue.add(jsonObjectRequest);




        return ret;

    }



    @Override
    public void onStop() {
        super.onStop();
        mRequestQueue.cancelAll(getActivity());
    }

    @Override
    public void onClick(View v) {
        String tag = (String) v.getTag();
        String comicId = null;
        String name = null;
        int childPosition;
        Comic itemData;
        switch (tag){
            case "recommend":
                childPosition = recommendRecycle.getChildPosition(v);
                itemData = recommendAdapter.getItemData(childPosition);
                comicId = itemData.getId();
                name = itemData.getComic_name();
                break;
            case "new":
                childPosition = newRecycle.getChildPosition(v);
                itemData = newAdapter.getItemData(childPosition);
                comicId = itemData.getId();
                name = itemData.getComic_name();
                break;
            case "hot":
                childPosition = hotRecycle.getChildPosition(v);
                itemData = hotAdapter.getItemData(childPosition);
                comicId = itemData.getId();
                name = itemData.getComic_name();
                break;
            case "like":
                childPosition = likeRecycle.getChildPosition(v);
                itemData = likeAdapter.getItemData(childPosition);
                comicId = itemData.getId();
                name = itemData.getComic_name();
                break;
        }

        Intent intent = new Intent(getActivity(), NewChapterActivity.class);
        String infoUrl = UrlMaker.GetComicInfor(comicId);
        intent.putExtra("url", infoUrl);
        intent.putExtra("name",name);
        startActivity(intent);

    }
}
